package org.mineground.commands.player.admin;

import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jibble.pircbot.Colors;
import org.mineground.Main;
import org.mineground.PermissionNodes;
import org.mineground.Utilities;

public class Time implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) throws CommandException {
	Player player = (Player) sender;

	if (!player.hasPermission(PermissionNodes.PERMISSION_TIME_COMMAND)) {
            return true;
        }

	if (args.length == 0) {
	    player.sendMessage(ChatColor.RED + "* Usage: '/time <day/night>'");
	    return true;
	}
        
        if (!player.hasPermission(PermissionNodes.PERMISSION_TIME_DELAY_OVERRIDE)) {
            if (System.currentTimeMillis() < Main.getInstance().lastTimeTick + Main.getInstance().getConfigHandler().getTimeDelay() * 60000L && Main.getInstance().lastTimeTick != 0) {
                long restTime = Main.getInstance().getConfigHandler().getTimeDelay() * 60 - ((System.currentTimeMillis() - Main.getInstance().lastTimeTick) / 1000);
                player.sendMessage(ChatColor.RED + "* Error: There's a delay on '/time' and you have to wait another " + Utilities.formatTime(restTime));
                return true;
            }
        }

        Main.getInstance().lastTimeTick = System.currentTimeMillis();
	long time = player.getWorld().getTime();
	time = time - time % 24000;

	if (args[0].equalsIgnoreCase("day")) {
	    World playerWorld = player.getWorld();
	    StringBuilder output = new StringBuilder();
	    
	    playerWorld.setTime(time + 24000);
	    
	    output.append(ChatColor.GRAY);
	    output.append("* ");
	    output.append(player.getDisplayName());
	    output.append(ChatColor.GRAY);
	    output.append(" has changed the time in ");
	    output.append(playerWorld.getName());
	    output.append(" to day.");
	    Utilities.sendMessageToAll(output.toString());
	    
	    output = new StringBuilder();
	    
	    output.append(Colors.DARK_GREEN);
	    output.append("* ");
	    output.append(Utilities.fixName(player));
	    output.append(Colors.DARK_GREEN);
	    output.append(" has changed the time in ");
	    output.append(playerWorld.getName());
	    output.append(" to day.");
	    
	    Main.getInstance().getIRCHandler().sendMessage(Main.getInstance().getConfigHandler().getIrcChannel(), output.toString());
	}

	else if (args[0].equalsIgnoreCase("night")) {
	    World playerWorld = player.getWorld();
	    StringBuilder output = new StringBuilder();
	    
	    playerWorld.setTime(time + 37700);
	    
	    output.append(ChatColor.GRAY);
	    output.append("* ");
	    output.append(player.getDisplayName());
	    output.append(ChatColor.GRAY);
	    output.append(" has changed the time in ");
	    output.append(playerWorld.getName());
	    output.append(" to night.");
	    Utilities.sendMessageToAll(output.toString());
	    
	    output = new StringBuilder();
	    
	    output.append(Colors.DARK_GREEN);
	    output.append("* ");
	    output.append(Utilities.fixName(player));
	    output.append(Colors.DARK_GREEN);
	    output.append(" has changed the time in ");
	    output.append(playerWorld.getName());
	    output.append(" to night.");
	    
	    Main.getInstance().getIRCHandler().sendMessage(Main.getInstance().getConfigHandler().getIrcChannel(), output.toString());
	}
	

	else {
	    player.sendMessage(ChatColor.RED + "* Usage: '/time <day/night>'");
	    return true;
	}

	return true;
    }
	
}
    
    
